//var interfaceMobileUrl="http://192.168.8.1:8080/ReaderHome/interface/mobile";
var interfaceMobileUrl="http://120.24.79.157/interface/mobile";
var userid=null;window.localStorage.getItem("userid");
var userkey=null;window.localStorage.getItem("userkey");
var city = window.localStorage.getItem("city");
var indexPage=1;
var myScroll=null;
var assetsUrl = "";
var point = null;
var isGoingToExist=false;
var addressSetMap=null;
var oldMarker=null;
var version=3;
//window.location.href="http://120.24.79.157/";

$(function(){
	addScript("http://api.map.baidu.com/api?v=2.0&ak=syGT7VOgLy4UdPYlSGaul5sy&callback=mapLoaded");
	
	setTimeout("personalInfo()",500);
	
	addScript(assetsUrl+"js/server/personalInfo.js");
	addScript(assetsUrl+"js/server/loadUdls.js");
	addScript(assetsUrl+"js/server/releaseDetail.js");
	addScript(assetsUrl+"js/server/sumary.js");
	
//	window.open("http://120.24.79.157/apk/ReaderHome.apk", "_system");
	$("#checkUpdate").click(function(){
		$.get("http://api.interlib.com.cn:8586/InterlibCommonService/admin/appUpdateInfo/getLatest?platform=android&appName=ReaderHome", function(result){
		    var latestVersion = result.versionCode;
		    if(latestVersion > version){
		    	var update=confirm("检查到新版本，确定要更新吗");
		    	if(update){
		    		window.open("http://api.interlib.com.cn:8586/InterlibCommonService/android/getLatestVesion?appName=ReaderHome", "_system");
		    	}
		    }else{
		    	xfAlert("已经是最新版本");
		    }
		});
	});
	
//	cordova.exec(function(data){alert(data);}, function(w){xfAlert("失败");}, "makePhoneCall", "call", ["hehe"]);
	
});

//等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);  


// PhoneGap加载完毕，可以安全调用PhoneGap方法
function onDeviceReady() {
//	$.ui.loadContent("#index",false,false,"up");
	checkConnection(); 
	document.addEventListener("offline", onOffline, false);
	document.addEventListener("backbutton", onBackKeyDown, false);
	document.addEventListener("pause", onPause, false);
	document.addEventListener("resume", onResume, false);
	$("#exit").click(function(){
		navigator.app.exitApp();
	});
}

function onPause() {
	stopWatch();
}
function onResume() {
	var href = window.location.href;
	if(href.indexOf("#yaoyiy")>0){
		startWatch();
	}
}
function onOffline() { 
	xfAlert("您的网络似乎不稳定");
}
function checkConnection() { 
	var networkState = navigator.network.connection.type; 		 
	if(networkState == Connection.NONE){
		xfAlert("网络连接失败，请检查您的网络");
	}
}
function onBackKeyDown() {
	var link = window.location.href;
	var menuState = $(".afui_panel_mask").css("display");
	if(menuState == "block"){
		if(isGoingToExist){
			navigator.app.exitApp();
		}
		isGoingToExist = true;
		$('.tishikuan p').html("再按一次退出");
		$('.tishikuan').css({'bottom':'7em'});
		var tstime=setTimeout(function (){
			$(".tishikuan").css({'bottom':'-10em'});
			isGoingToExist = false;
		}, 3000);
		return;
	}
	
	if(link.indexOf("#")<0||link.indexOf("me_shu")>0||link.indexOf("fujinzj")>0||link.indexOf("zjiels")>0||link.indexOf("index")>0||link.indexOf("jieyuels")>0||link.indexOf("denglu")>0||link.indexOf("shezhi")>0||link.indexOf("lijizj")>0||link.indexOf("fabzj")>0){
		af.ui.toggleSideMenu();
	}else if(link.indexOf("bdduzz")>0||link.indexOf("sjyz")>0||link.indexOf("huoyuedzStep1")>0){
		$.ui.loadContent("#shezhi",false,false,"up");
	}else if(link.indexOf("yaoyiy")>0){
		$.ui.loadContent("#index",false,false,"up");
		af.ui.toggleSideMenu();
	}else{
		$.ui.goBack();
	}
}

function pjjyr_load(){
	$("#pjjyr input").each(function(index,element){
		$(element).removeClass("cur");
	});
}

function mapLoaded(){
	
	navigator.geolocation.getCurrentPosition(onSuccess, onError);
	function onSuccess(position) {
		var latitude = position.coords.latitude; 
		var longitude = position.coords.longitude; 
		var gpsPoint = new BMap.Point(longitude, latitude);
        translateCallback = function (baiduPoint){
        	point = baiduPoint;
    	};
    	BMap.Convertor.translate(gpsPoint,0,translateCallback);	
//			var location = 'Latitude: '           + position.coords.latitude              + '<br />' +
//								'Longitude: '          + position.coords.longitude             + '<br />' +
//								'Altitude: '           + position.coords.altitude              + '<br />' +
//								'Accuracy: '           + position.coords.accuracy              + '<br />' +
//								'Altitude Accuracy: '  + position.coords.altitudeAccuracy      + '<br />' +
//								'Heading: '            + position.coords.heading               + '<br />' +
//								'Speed: '              + position.coords.speed                 + '<br />' +
//								'Timestamp: '          + new Date(position.timestamp)          + '<br />';
	}
	function onError(error) {
		xfAlert("加载地理位置失败");
	}
	
	var myCity = new BMap.LocalCity();
	myCity.get(function(result){
		city = result.name;
//		alert(city);
		window.localStorage.setItem("city",city);
		loadDistricts();
	});
	addressSetMap = new BMap.Map("addressSetMap");
	addressSetMap.centerAndZoom(city, 17);
//	if(point == null){
//		addressSetMap.centerAndZoom(city, 17);
//	}
	addressSetMap.addEventListener("click",function(e){
		addMarker(e.point);
	});
}

function addMarker(markPoint){
	point = markPoint;
	if(oldMarker!=null){
    	addressSetMap.removeOverlay(oldMarker);
    }
	var marker = new BMap.Marker(markPoint);  // 创建标注
	addressSetMap.addOverlay(marker); 
	oldMarker=marker;
	var gc = new BMap.Geocoder();    
	gc.getLocation(markPoint, function(rs){
	     var addComp = rs.addressComponents;
	     var detail = addComp.street + addComp.streetNumber;
	     $("#dz_webk").html(detail);
	     district = addComp.district;
    	 city =  addComp.city;
    	 province = addComp.province;
    	 
    	 var label = new BMap.Label(detail,{offset:new BMap.Size(20,0)});
    	 marker.setLabel(label);
	 }); 
}
function addScript(src){
	var oHead = document.getElementsByTagName('HEAD').item(0); 
    var oScript= document.createElement("script"); 
    oScript.type = "text/javascript"; 
    oScript.src=src; 
    oHead.appendChild( oScript); 
}
function xfAlert(content){
//	$(document).unbind("click");
	
	$('.tishikuan p').html(content);
	$('.tishikuan').css({'bottom':'7em'});
	var tstime=setTimeout(function (){
		$(".tishikuan").css({'bottom':'-10em'});
	}, 10000);
	
//	setTimeout(function(){
	$(".tishikuan").click(function(){
		$(".tishikuan").css({'bottom':'-10em'});
	});
//	},2000);
	
}
function xfAlert3(content,time){
	$('.tishikuan p').html(content);
	$('.tishikuan').css({'bottom':'7em'});
	var tstime=setTimeout(function (){
		$(".tishikuan").css({'bottom':'-10em'});
	},3000);
}
function xfAlert2(content,time){
	$('.tishikuan p').html(content);
	$('.tishikuan').css({'bottom':'7em'});
	var tstime=setTimeout(function (){
		$(".tishikuan").css({'bottom':'-10em'});
	},2000);
}
function getNoneDataLi(tip){
	var none_data_li = "<li class='xf_loading removable'><img src='images/load_none.png'><p style='width: 100%'>"+tip+"</p> </li>";
	return none_data_li;
}
function getLoadingLi(tip){
	var none_data_li = "<li class='xf_loading removable'><img src='images/jzz.png'><p style='width: 100%'>"+tip+"</p> </li>";
	return none_data_li;
}
function loginSuccess(_userid,_password){
	$("#loginAndReg").css("display","none");
	var key = getMd5(_password);
	window.localStorage.setItem("userid", _userid);
	window.localStorage.setItem("userkey", key);
	userid = _userid;
	userkey = key;
	
	$("#loginAndReg").css("display","none");
	$("#loginStatus").html("");
	$("#loginStatus").append("<a href='#'>"+
			"<img src='"+assetsUrl+"images/toux.jpg'>"+
			"<p class='yh_name'></p>"+
			"<p></p>"+
			"</a>" +
	"<a class='zc_sz' href='#shezhi'><img src="+assetsUrl+"'images/icon_sz.png' ></a>");
	$.ui.loadContent("#index",false,false,"up");
}
function fabzj_load(){
	$("#huoyuedzStep1 header div a.hrefChange").attr("href","#fabzj");
}
function me_shu_load(){
	$("#huoyuedzStep1 header div a.hrefChange").attr("href","#me_shu");
}
